<?php
/**
 * xushuhui
 * 2017/9/2
 * 14:14
 */

namespace app\agentmobile\controller;

use app\admin\model\Article as ArticleModel;
use app\agentmobile\model\User;
use app\lib\validate\IDMustBePostiveInt;
use think\Controller;
use think\Request;
use Wx\Wechat;
use think\Session;
class Article extends Controller {
	// 素材中心
	public function index() {
		//$type1 = ArticleModel::where(array('status' => 1, 'type' => 1))->select();
		$wxData = [];
		$wxData['app_id'] = config('wx.app_id');
		$wxData['app_secret'] = config('wx.app_secret');
		$wxData['url'] = request()->url(true);
		$uid = session('uid');
		$wechatObj = Wechat::instance(config('wx.app_id'),config('wx.app_secret'));
		$type1 = $wechatObj->getMaterialList();
		$type2 = ArticleModel::where(array('status' => 1, 'type' => 2))->select();
		$type3 = ArticleModel::where(array('status' => 1, 'type' => 3))->select();
		$this->assign('uid',$uid);
		$this->assign('list1', $type1['item']);
		$this->assign('list2', $type2);
		$this->assign('list3', $type3);
		return view();
	}
	public function articleDetail() {
		$media_id = request()->param('media_id');
		$uid =  input('uid');
		$type = request()->param('type');
		$wxData = [];
		$wxData['app_id'] = config('wx.app_id');
		$wxData['app_secret'] = config('wx.app_secret');
		$wxData['url'] = request()->url(true);
		$wechatObj = Wechat::instance(config('wx.app_id'),config('wx.app_secret'));
		$wechatData = $wechatObj->shareWechat($wxData['url']);
		$contentData = $wechatObj->getOneMaterial($media_id);

		if($uid > 0){
			$user = User::get($uid);
			$contentData['mobile'] = $user['mobile'];
			$contentData['name'] = $user['name'];
		}

		$this->assign('data',$contentData);
		$this->assign('type',$type);
		$this->assign('wxData',array_merge($wxData,$wechatData));
		return view();
	}
	
	//图片详情
	public function imgDetail() {
		$id = Request::instance()->param('id');

		$wxData = [];
		$wxData['app_id'] = config('wx.app_id');
		$wxData['app_secret'] = config('wx.app_secret');
		$wxData['url'] = request()->url(true);
		$wechatObj = Wechat::instance(config('wx.app_id'),config('wx.app_secret'));
		$wechatData = $wechatObj->shareWechat($wxData['url']);

		$this->assign('data', ArticleModel::get($id));
		$this->assign('wxData',array_merge($wxData,$wechatData));
		return view();
	}
}